const app = getApp()
let db = wx.cloud.database()
Page({
  data: {
    address: {},
    cart: [], // 购物车
    detail: {},
    goodsId: "",
    activeitem: [{
        left: "分期",
        center: "开通任性付反60元红包",
        right: "去App激活>"
      },
      {
        top: "可参加一下优惠活动",
        text: "查看全部>",
        left: "促销",
        center: "预计反1云钻",
      },
      {
        left: "活动",
        center: "100%刮中券，最高50元无敌券",
        right: ">"
      },
      {
        left: "已选",
        center: "1件",
        right: ">"
      },
      {
        left: "活动",
        center: "100%刮中券，最高50元无敌券",
        right: ">"
      }
    ]
  },

  onLoad: function (opt) {
    let id = opt.id
    wx.cloud.database().collection('goodsList').doc(id).get()
      .then(res => {
        let detail = res.data;
        console.log("商品详情页", res.data)
        this.setData({
          detail,
          goodsId: id
        })
      }),
      // wx.cloud.database().collection('goodsList').doc(id).get()
      wx.cloud.database().collection('jujiadata').doc(id).get()
      .then(res => {
        let detail = res.data;
        console.log("商品详情页", res.data)
        this.setData({
          detail,
          goodsId: id
        })
      }),
      wx.cloud.database().collection('meishidata').doc(id).get()
      .then(res => {
        let detail = res.data;
        console.log("商品详情页", res.data)
        this.setData({
          detail,
          goodsId: id
        })
      }),
      wx.cloud.database().collection('fushidata').doc(id).get()
      .then(res => {
        let detail = res.data;
        console.log("商品详情页", res.data)
        this.setData({
          detail,
          goodsId: id
        })
      }),
      wx.cloud.database().collection('muyingdata').doc(id).get()
      .then(res => {
        let detail = res.data;
        console.log("商品详情页", res.data)
        this.setData({
          detail,
          goodsId: id
        })
      }),
      wx.cloud.database().collection('gehudata').doc(id).get()
      .then(res => {
        let detail = res.data;
        console.log("商品详情页", res.data)
        this.setData({
          detail,
          goodsId: id
        })
      }),
      wx.cloud.database().collection('shumadata').doc(id).get()
      .then(res => {
        let detail = res.data;
        console.log("商品详情页", res.data)
        this.setData({
          detail,
          goodsId: id
        })
      }),
      wx.cloud.database().collection('yundongdata').doc(id).get()
      .then(res => {
        let detail = res.data;
        console.log("商品详情页", res.data)
        this.setData({
          detail,
          goodsId: id
        })
      })

    this.getGoodDetail()
  },
  //获取商品详情
  getGoodDetail() {
    //获取购物车商品i
    let cart = wx.getStorageSync('cart') || [];
    db.collection('goodsList').doc(this.data.goodsId).get()
    db.collection('jujiadata').doc(this.data.goodsId).get()
    db.collection('meishidata').doc(this.data.goodsId).get()
    db.collection('fushidata').doc(this.data.goodsId).get()
    db.collection('muyingdata').doc(this.data.goodsId).get()
    db.collection('gehudata').doc(this.data.goodsId).get()
    db.collection('shumadata').doc(this.data.goodsId).get()
    db.collection('yundongdata').doc(this.data.goodsId).get()
      .then(res => {
        console.log('获取商品详情成功', res)
        let good = res.data
        this.setData({
          cart: cart,
          good: good
        })
      })

    // .then(res => {
    //   console.log('获取商品详情成功', res)
    //   let good = res.data
    //   this.setData({
    //     cart: cart,
    //     good: good
    //   })
    // })
  },


  // 加入购物车
  handleCartAdd() {
    let cart = wx.getStorageSync('cart') || [];
    console.log("购物车中已存在的数据：");
    console.log(cart);
    //获取当前商品的所有数据
    let detail = this.data.detail;
    // 如果购物车中商品的id和当前商品的id一样
    let index = cart.findIndex(v => v._id === detail._id);
    if (index === -1) {
      // 若是这里没有返回则添加的是新的商品
      detail.num = 1;
      detail.checked = true;
      cart.push(detail);
    } else {
      // 说明购物车中有当前商品 此时我们加数量就行
      cart[index].num++;
    }
    wx.setStorageSync('cart', cart);
    wx.showToast({
      title: '加入购物车成功',
      icon: "sucess",
      mask: true,
    })
  },
  //直接购买
  handlePay() {
    let cart = wx.getStorageSync('cart') || [];
    //获取当前商品的所有数据
    let detail = this.data.detail;

    // 如何从购物车中商品的id和当前商品的id一样
    let index = cart.findIndex(v => v._id === detail._id);
    if (index === -1) {
      // 若是这里没有返回则添加的是新的商品
      detail.num = 1;
      detail.checked = true;
      cart.push(detail);
    }
    wx.setStorageSync('cart', cart);

  },

})